const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
Page({

     /**
      * 页面的初始数据
      */
     data: {
          inputShowed: false,
          inputVal: "",

          search: false, //判断模糊查询方法
          total: '0',
          myAgentList: {
               hasMore: true,
               list: [],
               listSize: 0
          },
          startNum: 1, //页数

          curisOnSale: '',

          data: {
               deptId: '', // 商家id
               isOnSale: 1, //必选  是否上架
               orderById: 1, //必选  排序编号
               goodsName: '', // 可选  商品名
          },

          listnum: 10, //分页列表个数
          sysDeptList: [],
          sysDeptIndex: 0,
          goodsList: [], //服务列表
          marketInfo: {} // 全部信息
     },
     goodsDeviceList: function (buessinessId) {
          var that = this;
          util.request(api.goodsDeviceList, {
               'buessinessId': buessinessId, //商家id
          }, 'GET').then(function (res) {
               that.setData({
                    goodsList: res
               });
               console.log(JSON.stringify(res));
          });

     },
     maginfo: function (e) {
          wx.navigateTo({
               url: '/pages/goods/goodservice?id=' + e.currentTarget.id,
          })
     },

     //上拉获取更多列表
     hotActivityLoadMore: function (e) {
          var that = this
          var search = that.data.search
          if (search) {
               that.listPage(that.data.inputVal)
          } else if (!search) {
               that.listPage('')
          }

     },

     //分页查询列表
     listPage: function (goodsName) {
          var that = this;
          //判断是否还有下一页
          if (that.data.myAgentList.hasMore == true) {
               wx.showLoading({
                    title: '加载中',
               })

               var num = that.data.listnum
               var pageNum = that.data.startNum
               var url = 'current=' + pageNum + '&size=' + num
               var data = that.data.data
               data.goodsName = goodsName

               util.requestNoToken(api.AllGoodList + url, data, "POST").then(function (res) {
                    wx.hideLoading();
                    if (res.success) {
                         var listSize = res.obj.records.length;
                         var newData = that.data.myAgentList.list;
                         var lis = res.obj.records;
                         lis.forEach(function (item, index, array) {
                              array[index] = {
                                   id: item.id,
                                   primaryPicUrl: item.primaryPicUrl,
                                   goodsName: item.goodsName,
                                   marketPrice: item.marketPrice,
                                   retailPrice: item.retailPrice,
                                   categoryName: item.categoryName,
                                   sellVolume: item.sellVolume,
                                   goodsBrief: item.goodsBrief
                              }
                         });
                         //concat是在原有基础上进行增加
                         newData = newData.concat(lis);
                         var newList = {};
                         //当长度等于于0表示当前没有数据，停止
                         if (listSize == 0) {
                              newList.hasMore = false;
                              wx.showToast({
                                   title: "已加载全部列表",
                                   icon: 'none',
                                   duration: 2000
                              });
                              return;
                              //当长度小于10表示没有下页数据了
                         } else if (listSize < num) {
                              wx.showToast({
                                   title: "已加载全部列表",
                                   icon: 'none',
                                   duration: 2000
                              });
                              newList.hasMore = false;
                         } else {
                              newList.hasMore = true;
                         }
                         newList.list = newData;
                         newList.listSize = listSize;
                         that.setData({
                              myAgentList: newList
                         });

                         that.setData({
                              startNum: pageNum + 1 //页数加1
                         })
                    } else {
                         console.log("获取全部商品列表失败")
                    }
               }).catch(function (res) {
                    wx.hideLoading();
                    console.log("获取数据失败！")
               })
          } else {
               wx.showToast({
                    title: "已加载全部商品",
                    icon: 'none',
                    duration: 2000
               });
          }
     },

     //获取商品列表初始值
     getDeviceOrderList: function () {
          var that = this;
          console.log("获取套餐");
          var deptId = that.data.deptId;
          var pageNum = that.data.startNum
          var url = 'current=' + pageNum + '&size=';
          //获取首页套餐服务列表
          util.request(api.ShopGoodList, {
               'userId': deptId, // 商家id
               'status': '1', //类型
          }, 'POST').then(function (res) {
               console.log(res);
               that.setData({
                    goodsList: res.records,
               });
          }).catch(function (res) {
               console.log("请求失败！")
          })
     },
     // 获取商店列表和详情初始化severList
     getmarketInfo: function () {
          var that = this;
          console.log("获取套餐");
          var deptId = that.data.deptId;
          //获取首页套餐服务列表
          util.request(api.severList + deptId, {
          }, 'GET').then(function (res) {
               console.log(res);
               that.setData({
                    marketInfo: res,
               });
          }).catch(function (res) {
               console.log("请求失败！")
          })
     },

     newDataSet: function () {
          var that = this
          var data = {
               isOnSale: 1, //必选  是否上架
               orderById: 1, //必选  排序编号
               goodsName: '', // 可选  商品名
          }
          var myAgentList = {
               hasMore: true,
               list: [],
               listSize: 0,
          }

          //数据初始化
          that.setData({
               data: data,
               myAgentList: myAgentList,
               startNum: 1
          })
     },




     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          var that = this
          // 赋值
          that.setData({
               deptId: options.deptId
          })

          that.sysDeptList();
          // 获取所有套餐
          that.getmarketInfo();
          that.getDeviceOrderList();
     },

     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function () {

     },

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function () {

     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function () {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function () {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {

     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     },
     /**
      * 根据类型查询所有商家
      * 
      */
     sysDeptList: function () {
          var that = this;
          util.request(api.sysDeptList, {
               'deptType': '3', //类型
          }, 'GET').then(function (res) {
               that.setData({
                    sysDeptList: res
               });
               console.log(JSON.stringify(res));
          });
     },
     /**
      * 根据类型查询所有商家
      * 
      */
     sysDeptChange: function (e) {
          console.log('picker country 发生选择改变，携带值为', e.detail.value);
          this.setData({
               sysDeptIndex: e.detail.value
          })
     }
})